package com.wutianyi.wutianyi86728_3.json;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Map;

import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.DeserializationConfig.Feature;
import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion;

public class Json {
	private static String str = " {\"name\" : { \"first\" : \"Joe\", \"last\" : \"Sixpack\" },\"gender\" : \"MALE\",\"verified\" : false,\"userImage\" : \"Rm9vYmFyIQ==\"}";
	
	/**
	 *对ObjectMapper设置 
	 */
	public Json() {
		ObjectMapper mapper = new ObjectMapper();
		DateFormat dateFormat = new SimpleDateFormat("YYYY-MM-DD : HH:mm:ss");
		//设置包含对象中的所有的属性
		mapper.getSerializationConfig().setSerializationInclusion(Inclusion.ALWAYS);
		//设置对时间类型的处理
		mapper.getSerializationConfig().setDateFormat(dateFormat);
		
		mapper.getDeserializationConfig().set(Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		mapper.getDeserializationConfig().setDateFormat(dateFormat);
	}
	
	public enum Gender {
		MALE, FEMALE
	};

	public static class Name {
		private String _first, _last;

		public String getFirst() {
			return _first;
		}

		public String getLast() {
			return _last;
		}

		public void setFirst(String s) {
			_first = s;
		}

		public void setLast(String s) {
			_last = s;
		}
	}

	private Gender gender;
	private Name name;
	private boolean isVerified;
	private byte[] userImage;

	public Gender getGender() {
		return gender;
	}

	public void setGender(Gender gender) {
		this.gender = gender;
	}

	public Name getName() {
		return name;
	}

	public void setName(Name name) {
		this.name = name;
	}

	public boolean isVerified() {
		return isVerified;
	}

	public void setVerified(boolean isVerified) {
		this.isVerified = isVerified;
	}

	public byte[] getUserImage() {
		return userImage;
	}

	public void setUserImage(byte[] userImage) {
		this.userImage = userImage;
	}

	public static void treeModel() throws JsonParseException, JsonMappingException, IOException {
		ObjectMapper m = new ObjectMapper();
		JsonNode rootNode = m.readValue(str, JsonNode.class);
		
		JsonNode nameNode = rootNode.path("name");
		System.out.println(nameNode.path("last").getTextValue());
	}

	public static void treeMapper() {
	}
	
	public static void main(String[] args) throws JsonParseException,
			JsonMappingException, IOException {
		
		ObjectMapper mapper = new ObjectMapper();
		mapper.getSerializationConfig().setSerializationInclusion(Inclusion.ALWAYS);
		
		Json json = mapper.readValue(str, Json.class);
		Map<String, Object> userData = mapper.readValue(str, Map.class);
		System.out.println(mapper.writeValueAsString(json));
		System.out.println();
	}
}
